/**
 * OverView: 数字、日期、文本、页面元素等输出格式转换
 * Author: Zhaomi
 * Date: 13-5-13 下午8:16
 * Email: yanyuetian@gmail.com
 * Create By JetBrains WebStorm.
 */
exports.getShortNum = function(number){
    var charAry = ["亿","万"],
        numAry = [100000000,10000];
    for(var i=0;i<numAry.length;i++){
        var fair = number%numAry[i],
            itInt = number/numAry[i];
        if(itInt>1)
            return  parseInt(itInt)+charAry[i]+(fair!=0?"+":"");
    }

    if(number<=5000){
        return 2500+(number%5000!=0?"+":"");
    }
    if(number>5000 && number<10000){
        return 5000+"+";
    }
};

//包大小后缀格式化
exports.byteUnit = function(len){
    if(typeof(len) == "undefined"){
        return "";
    }
    if(len<1024)
        return len.toFixed(1)+"B";
    else if(len<1048576)
        return (len/1024).toFixed(1)+"K";
    else if(len<1073741824)
        return (len/1048576).toFixed(1)+"M";
    else
        return (len/1073741824).toFixed(1)+"G";
};

exports.getScoreAry = function getScoreAry(score){
    var resAry = [];
    if(score > 5){
        score = 5;
    }
    while(score >= 1){
        resAry[resAry.length] = "good";
        score--;
    }
    if(score > 0){
        resAry[resAry.length] = "soso";
    }
    while(resAry.length < 5){
        resAry[resAry.length] = "";
    }

    return resAry;
};


exports.htmlDecode =function(str) {
    if(!str)
        return;
    var strLen = str.length,
        lastFourChars = "",
        currCharCode = "",
        resstr = "";
    for(var k=0;k<strLen;k++){
        var currChar = str[k];
        if(currChar == "&" || currChar == "#")
            continue;
        if(currChar == ";"){
            var decodeChar = String.fromCharCode(parseInt(currCharCode));
            if(decodeChar == "<" || lastFourChars)   /*html标签开头，则记下四个前缀*/
                lastFourChars += decodeChar;
            else
                resstr += decodeChar;
            if(lastFourChars.length >= 4){

                resstr += lastFourChars;
                lastFourChars = "";
            }
            currCharCode = "";
        }else
            currCharCode += currChar;
    }
    return resstr;
    /*return str.replace(/&#(x)?([^&]{1,5});?/g,function($,$1,$2) {
     return String.fromCharCode(parseInt($2 , $1 ? 16:10));
     });*/
};

